﻿// ----------------------------------------------------------------
//  Copyright (C) 2018 奇瑞信息有限公司
//  版权所有。 
//  文件名：UserLoginService
//  文件功能描述：用户登录 后台接口
//  创建人：陈明华
//  创建时间：2018-12-03
// ----------------------------------------------------------------
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Runtime.Caching;
using Abp.UI;
using CheryABP.ApiAttribute;
using CheryABP.Common;
using CheryABP.Extensions;
using CheryABP.SystemSetitng;
using CheryABP.SystemSetting.LoginAPI.Dto;
using Microsoft.AspNetCore.Http;
using System;

namespace CheryABP.SystemSetting.LoginAPI
{

    /// <summary>
    /// 用户登录api
    /// </summary>
    public class UserLoginService :IUserLoginService
    {

        private readonly IRepository<system_login> _loginRepository;
        private readonly ICacheManager _cacheManager;
        /// <summary>
        /// 登录人信息
        /// </summary>
        private readonly IHttpContextAccessor _httpContextAccessor;
        private ISession _session => _httpContextAccessor.HttpContext.Session;

        /// <summary>
        /// 构造函数自动注入
        /// </summary>
        /// <param name="loginRepository"></param>
        /// <param name="cacheManager"></param>
        /// <param name="httpContextAccessor"></param>
        public UserLoginService(IRepository<system_login> loginRepository, ICacheManager cacheManager, IHttpContextAccessor httpContextAccessor)
        {
            _loginRepository = loginRepository;
            _cacheManager = cacheManager;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 登录操作
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
     
        public string GetLoginInfo(string userName,string password)
        {
            try
            {
                string pwd = DESEncrypt.Encrypt_pw(password);
                var loginModel = _loginRepository.FirstOrDefault(o => o.user_name.Equals(userName) && o.password.Equals(pwd) && o.stop_status==1);
                if (loginModel != null)
                {
                    // Utils.SetSession(_session, DTKeys.SESSION_ADMIN_INFO,userName);
                    AbpSessionExtension.SetObjectAsJson(_session, DTKeys.SESSION_ADMIN_INFO, loginModel);
                    _cacheManager.GetCache(DTKeys.LOGINCACHE).SetAsync(userName, loginModel, TimeSpan.FromDays(1));//设置一天缓存时间
                    return "登录成功";
                }
                throw new UserFriendlyException("登录失败！");
            }
            catch (Exception ex)
            {
                throw new UserFriendlyException(ex.Message);
            }
             
        }
        /// <summary>
        /// 根据工号查询人员信息
        /// </summary>
        /// <param name="jobNo">工号</param>
        /// <returns></returns>
        //public UserDto GetUserInfo(string jobNo)
        //{
        //    //string aa =  Utils.GetSession(_session, DTKeys.SESSION_ADMIN_INFO);
        //    //AdminInfo = AbpSessionExtension.GetObjectFromJson<system_login>(_session, DTKeys.SESSION_ADMIN_INFO);
      
        //    var cacheModel = (system_login)_cacheManager.GetCache(DTKeys.LOGINCACHE).GetOrDefault(jobNo);
        //    system_login model = cacheModel == null ? _loginRepository.FirstOrDefault(o => o.job_number.Equals(jobNo)) : cacheModel;
        //    return model.MapTo<UserDto>();
        //}
    }
}
